package com.xinchuang;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.xinchuang.common.KeyWordSystem;
import com.xinchuang.mapper.targetSystem.ZoneMapper;
import com.xinchuang.entity.targetSystem.Zone;
import com.xinchuang.service.OldDataService;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.targetSystem.CommonService;
import com.xinchuang.service.targetSystem.TargetTypeService;
import com.xinchuang.service.targetSystem.config.SystemConfigService;
import com.xinchuang.service.targetSystem.config.SystemParamService;
import com.xinchuang.service.targetSystem.target.SystemIndexService;
import com.xinchuang.util.DBConManager;
import com.xinchuang.util.FileElseUtil;
import com.xinchuang.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * TODO 初始化
 *
 * @author zkwczx
 * @version 1.0
 * @date 2021/3/19 10:18
 */
@Order(value = 2)
@Slf4j
@Component
public class TargetManageRun implements ApplicationRunner {

    public static Boolean isInit = false;

    @Resource
    private CommonService commonService;

    @Resource
    private TargetTypeService targetTypeService;

    @Resource
    private KeyWordSystem keyWordSystem;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private ZoneMapper zoneMapper;

    @Resource
    private SystemConfigService systemConfigService;

    @Resource
    private SystemParamService systemParamService;

    @Resource
    private RedisUseService redisUseService;

    @Resource
    private OldDataService oldDataService;

    @Resource
    private SystemIndexService systemIndexService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        FileUtil.mkdir(keyWordSystem.getFILE_URL());
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/sign");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/doc");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/file");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/local");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/cutOut");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/outDB");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/tif");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/toTif");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/tifElse");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/icon");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/image");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/type");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/zip");
        FileUtil.mkdir(keyWordSystem.getFILE_URL() + "/auto");
        log.info("所需文件路径创建成功");
        /**
         DBConManager.createDB(keyWordSystem.getFILE_URL());
         DBConManager.createLogDB(keyWordSystem.getFILE_URL());
         log.info("sqlite数据库创建成功");
         **/
        commonService.initializeCommonMainData();
        log.info("初始化共有属性成功");
        commonService.initializeCommonSubData();
        log.info("初始化子共有属性成功");
        targetTypeService.initialize();
        log.info("初始化类型父类名称成功");
        commonService.initTypeSign();
        log.info("初始化类型默认图片成功");
        systemConfigService.init();
        log.info("初始化体系标记线成功");
        redisUtil.del(keyWordSystem.getVERSION() + "country");
        redisUtil.lSet(keyWordSystem.getVERSION() + "country", FileElseUtil.countryList());
        log.info("初始化国家信息成功");
        systemParamService.initSystemSort();
        log.info("初始化系统/体系排序成功");
        systemParamService.initSytemParam();
        log.info("初始化系统参数成功");
        systemParamService.initElement();
        log.info("初始化要素编辑成功");
        redisUseService.initTypeSign();
        log.info("初始化目标类型图标成功");
        redisUseService.initTypeName();
        log.info("初始化目标类型名称成功");
        redisUseService.initTarget();
        redisUseService.initSub();
        redisUseService.initTargetBase();
        redisUseService.initSubBase();
        redisUseService.initTargetSubRelation();
        redisUseService.initPostureData();
        redisUseService.initPostureRelation();
        log.info("初始化目标基础数据成功");
        redisUseService.initSystemsTargetList();
        redisUseService.initSystemsRelationRangeValue();
        log.info("初始化体系数据成功");
        redisUseService.initView();
        log.info("初始化场景数据成功");
        redisUseService.initRangeType();
        log.info("初始化作用类型数据成功");
        systemIndexService.initTargetOrSubLevelRedisData();
        log.info("初始化作用指标数据成功");
        commonService.initFontData();
        log.info("初始化字体参数成功");
        /**
         * TODO: 旧数据的适配 start
         */
//        oldDataService.oldToNewData();
        systemIndexService.oldLevel();
        /**
         * TODO: 旧数据的适配 end
         */
        try {
            if (!redisUtil.hasKey(keyWordSystem.getVERSION() + "-REGION")) {
                List<Zone> zoneCountry = zoneMapper.getZoneAll(Arrays.asList("country"));
                List<Zone> zoneElse = zoneMapper.getZoneAll(Arrays.asList("province", "city", "district"));
                if (CollectionUtil.isNotEmpty(zoneCountry)) {
                    Map<String, Object> map;
                    for (Zone zone : zoneCountry) {
                        map = new HashMap<>();
                        map.put("value", zone.getAdcode());
                        map.put("label", zone.getName());
                        map.put("region", new HashMap<>());
                        if (zone.getName().equals("中国") && CollectionUtil.isNotEmpty(zoneElse)) {
                            map.put("children", getTreeChild(zone.getAdcode(), zoneElse));
                        }
                        redisUtil.lSet(keyWordSystem.getVERSION() + "-REGION", map);
                    }
                }
            }
        } catch (Exception e) {

        }
        isInit = true;
        log.info("***************INITIALIZE IS SUCCESS***************");
    }

    /**
     * 递归查询国家树
     *
     * @param id
     * @param allTree
     * @return
     */
    private List<Map<String, Object>> getTreeChild(String id, List<Zone> allTree) {
        /**
         * 子菜单
         */
        List<Map<String, Object>> childList = new ArrayList<>();
        for (Zone zone : allTree) {
            if (StringUtils.isNotBlank(zone.getParent())) {
                if (id.equals(zone.getParent())) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("value", zone.getAdcode());
                    map.put("label", zone.getName());
                    childList.add(map);
                }
            }
        }
        /**
         * 递归
         */
        for (Map<String, Object> map : childList) {
            String aId = (String) map.get("value");
            map.put("children", getTreeChild(aId, allTree));
        }
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

}
